import { Tree } from 'antd';
import React from 'react';
import { connect } from 'dva';

const TreeNode = Tree.TreeNode;

@connect(({ add, theme }) => ({
    add: add,
    theme: theme
}))
export default class TreeList extends React.Component {
    /**
     * 0 视频
     * 1 资讯
     * 2 直播
     * 3 专题
     */
    state = {
        activeModal: '',
        plate_id: ''
    }

    componentDidMount() {
        this.props.onRef(this)
    }

    onSelect = (selectedKeys, info) => {
        if(info.selectedNodes[0]){
            let key = info.selectedNodes[0].key
            let data = info.selectedNodes[0].props
            let keyArr = key.split("-")
            // console.log(data)
            //视频
            if (keyArr[0] == 0) {
                let dataAugment = {
                    index: 1,
                    status: 2,
                    page_size: 10,
                    plate_id: data.plate_id,
                }
                
                this.props.dispatch({ type: 'add/getTableListVideo', payload: { dataAugment: dataAugment } });
                this.props.dispatch({ type: 'add/saveBannerPlateID', data: data.plate_name });
                this.setState({
                    activeModal: 0,
                    plate_id: data.plate_id
                })
            }
            //资讯
            if (keyArr[0] == 1) {
                let dataAugment = {
                    index: 1,
                    status: 2,
                    page_size: 10,
                    plate_id: data.plate_id,
                    module_name:1
                }
                this.props.dispatch({ type: 'add/getTableList', payload: { dataAugment: dataAugment } });
                this.setState({
                    activeModal: 1,
                    plate_id: data.plate_id
                })
            }
            //专题
            if (keyArr[0] == 3) {
                let dataAugment = {
                    index: 1,
                    page_size: 10
                }
                this.props.dispatch({ type: 'add/themeList', payload: { dataAugment: dataAugment } });
                this.setState({
                    activeModal: 3
                })
            }
            //发现
            if (keyArr[0] == 4) {
                let dataAugment = {
                    index: 1,
                    page_size: 10
                }
                this.props.dispatch({ type: 'add/serviceList', payload: { dataAugment: dataAugment } });
                this.setState({
                    activeModal: 4
                })
            }
            this.props.resetIndex()
        }
    }

    render() {
        const { treeList, treeListVideo } = this.props.add
        const treeData = [{
            title: '视频组件',
            key: '0',
            children: treeListVideo
        },
        {
            title: '资讯组件',
            key: '1',
            children: treeList
        },
        // {
        //     title: '直播组件',
        //     key: '2',
        //     children: []
        // }, 
        // {
        //     title: '专题模块',
        //     key: '3',
        //     children: []
        // },{
        //     title: '服务模块',
        //     key: '4',
        //     children: []
        // }
    ]
        // if (this.props.theme.params.id) {
        //     treeData.push({
        //         title: '资讯组件',
        //         key: '1',
        //         children: treeList
        //     })
        // }
        const loop = datas => datas.map((item) => {
            if (item.children && item.children.length) {
                return <TreeNode key={item.key} title={item.title} plate_id={item.plate_id} 
                plate_name={item.plate_name} disabled={true}>{loop(item.children)}</TreeNode>;
            }
            return <TreeNode key={item.key} plate_name={item.plate_name} title={item.title} plate_id={item.plate_id} />;
        });
        return (
            <Tree
                showLine
                defaultExpandedKeys={['0-0-0']}
                onSelect={this.onSelect}
                defaultExpandParent
                autoExpandParent
            >
                {loop(treeData)}
            </Tree>
        );
    }
}
